//Original code by John Loomis,
module fallDownFPGA(
//	Clock Input
  input CLOCK_50,	//	50 MHz
  input CLOCK_27, // 27 MHz
//	Push Buttons
  input [3:0] KEY,
//	Switches
  input [17:0] SW,
//	7-SEG Display
  output [6:0]	HEX0,HEX1,HEX2,HEX3,
//	TV Decoder Reset
  output TD_RESET,	
// VGA
  output VGA_CLK,   						//	VGA Clock
  output VGA_HS,							//	VGA H_SYNC
  output VGA_VS,							//	VGA V_SYNC
  output VGA_BLANK,						//	VGA BLANK
  output VGA_SYNC,						//	VGA SYNC
  output [9:0] VGA_R,   						//	VGA Red[9:0]
  output [9:0] VGA_G,	 						//	VGA Green[9:0]
  output [9:0] VGA_B   						//	VGA Blue[9:0]
);

//assigning wires to keys, not Loomis
wire RST;
wire k1;
wire k2;
assign k1 = KEY[1];
assign k2 = KEY[3];
assign RST = KEY[0];

// reset delay gives some time for peripherals to initialize
wire DLY_RST;
Reset_Delay r0(	.iCLK(CLOCK_50),.oRESET(DLY_RST) );

// Send switches to red leds 
assign LEDR = SW;

// Turn off green leds
assign LEDG = 8'h00;

wire [6:0] blank = 7'b111_1111;
//VGA wires
wire		VGA_CTRL_CLK;
wire		AUD_CTRL_CLK;
wire [9:0]	mVGA_R;
wire [9:0]	mVGA_G;
wire [9:0]	mVGA_B;
wire [9:0]	mCoord_X;
wire [9:0]	mCoord_Y;

VGA_Audio_PLL 	p1 (	
	.areset(~DLY_RST),
	.inclk0(CLOCK_27),
	.c0(VGA_CTRL_CLK),
	.c1(AUD_CTRL_CLK),
	.c2(VGA_CLK)
);

//instantiating game module, not Loomis
game u3(
	.CLOCK_50 (CLOCK_50),
	.key1 (k1),
	.key2 (k2),
	.key3 (SW[2]),
	.iCLK1(VGA_CLK),
	.iCLK2(VGA_CTRL_CLK),
	.px(mCoord_X),
	.py(mCoord_Y),
	.valid(valid),
	.pixel(pixel),
	.red(red),
	.green(green),
	.blue(blue),
	.HEX0(HEX0),
	.HEX1(HEX1),
	.HEX2(HEX2),
	.HEX3(HEX3)
);

wire valid, pixel;
wire r = valid & pixel & red;
wire g = valid & pixel & green;
wire b = valid & pixel & blue;

parameter WHITE = 10'h3FF;
parameter BLACK = 10'h000;

assign mVGA_R = (r? WHITE: BLACK);
assign mVGA_G = (g? WHITE: BLACK);
assign mVGA_B = (b? WHITE: BLACK);


vga_sync u1(
   .iCLK(VGA_CTRL_CLK),
   .iRST_N(DLY_RST&KEY[0]),	
   .iRed(mVGA_R),
   .iGreen(mVGA_G),
   .iBlue(mVGA_B),
   // pixel coordinates
   .px(mCoord_X),
   .py(mCoord_Y),
   // VGA Side
   .VGA_R(VGA_R),
   .VGA_G(VGA_G),
   .VGA_B(VGA_B),
   .VGA_H_SYNC(VGA_HS),
   .VGA_V_SYNC(VGA_VS),
   .VGA_SYNC(VGA_SYNC),
   .VGA_BLANK(VGA_BLANK)
);

//end of loomis code

endmodule